package com.ruoyi.population.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.population.domain.XlkjPopulation;
import com.ruoyi.population.service.IXlkjPopulationService;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.utils.DictStrUtils;
import com.ruoyi.utils.UrlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 人口Controller
 *
 * @author wang
 * @date 2022-01-11
 */
@RestController
@RequestMapping("/app/population/population")
public class XlkjPopulationControllerApp extends BaseController
{
    @Autowired
    private IXlkjPopulationService xlkjPopulationService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询人口列表
     */
//    @PreAuthorize("@ss.hasPermi('population:population:list')")
    @GetMapping("/list")
    public TableDataInfo list(XlkjPopulation xlkjPopulation)
    {
        startPage();
        if(xlkjPopulation.getDepartmentId() != null && xlkjPopulation.getDepartmentId() != ""){
            List<String> list = sysDeptMapper.selectIdByParentId(Long.valueOf(xlkjPopulation.getDepartmentId()));
            String str = "(";
            for(int i =0;i<list.size();i++){
                str += list.get(i);
                if(i != list.size()-1){
                    str += ",";
                }
            }
            str += ")";
            xlkjPopulation.setDepartmentList(str);
        }
        List<XlkjPopulation> list = xlkjPopulationService.selectXlkjPopulationList(xlkjPopulation);
        for(XlkjPopulation population: list){
            //图片路径拼接
            population.setPhoto(UrlUtils.getUrl() + population.getPhoto() );
            //部门值翻译
            String deptName = sysDeptService.selectDeptById(Long.valueOf(population.getDepartmentId())).getDeptName();
            population.setDepartmentId(deptName);
        }
        return getDataTable(list);
    }

    /**
     * 导出人口列表
     */
//    @PreAuthorize("@ss.hasPermi('population:population:export')")
//    @Log(title = "人口", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(XlkjPopulation xlkjPopulation)
    {
        List<XlkjPopulation> list = xlkjPopulationService.selectXlkjPopulationList(xlkjPopulation);
        for(XlkjPopulation population:list){
            //图片路径处理
            population.setPhoto(UrlUtils.getUrl() + population.getPhoto());
            //字典类型回显
            population.setCertificatesType(DictUtils.getDictLabel(DictStrUtils.CERTIFICATES_TYPE,population.getCertificatesType()));
            population.setSex(DictUtils.getDictLabel(DictStrUtils.SYS_USER_SEX,population.getSex()));
            population.setNation(DictUtils.getDictLabel(DictStrUtils.NATION_TYPE,population.getNation()));
            population.setSourceType(DictUtils.getDictLabel(DictStrUtils.SOURCE_TYPE,population.getSourceType()));
        }
        ExcelUtil<XlkjPopulation> util = new ExcelUtil<XlkjPopulation>(XlkjPopulation.class);
        return util.exportExcel(list, "人口数据");
    }

    /**
     * 人口导入
     */
//    @Log(title = "人口", businessType = BusinessType.IMPORT)
//    @PreAuthorize("@ss.hasPermi('system:user:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<XlkjPopulation> util = new ExcelUtil<XlkjPopulation>(XlkjPopulation.class);
        List<XlkjPopulation> userList = util.importExcel(file.getInputStream());
        for(XlkjPopulation population:userList){
            //字典类型翻译
            population.setCertificatesType(DictUtils.getDictValue(DictStrUtils.CERTIFICATES_TYPE,population.getCertificatesType()));
            population.setSex(DictUtils.getDictValue(DictStrUtils.SYS_USER_SEX,population.getSex()));
            population.setNation(DictUtils.getDictValue(DictStrUtils.NATION_TYPE,population.getNation()));
            population.setSourceType(DictUtils.getDictValue(DictStrUtils.SOURCE_TYPE,population.getSourceType()));
        }
        String message = xlkjPopulationService.importDevice(userList);
        return AjaxResult.success(message);
    }

    /**
     * 下载人口导入模板
     */
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<XlkjPopulation> util = new ExcelUtil<XlkjPopulation>(XlkjPopulation.class);
        return util.importTemplateExcel("人口数据");
    }

    /**
     * 获取人口详细信息
     */
//    @PreAuthorize("@ss.hasPermi('population:population:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        XlkjPopulation population = xlkjPopulationService.selectXlkjPopulationById(id);
        population.setCertificatesType(DictUtils.getDictLabel(DictStrUtils.CERTIFICATES_TYPE,population.getCertificatesType()));
        population.setSex(DictUtils.getDictLabel(DictStrUtils.SYS_USER_SEX,population.getSex()));
        population.setNation(DictUtils.getDictLabel(DictStrUtils.NATION_TYPE,population.getNation()));
        population.setSourceType(DictUtils.getDictLabel(DictStrUtils.SOURCE_TYPE,population.getSourceType()));
        if(population.getPhoto() != null){
            population.setPhoto(UrlUtils.getUrl() + population.getPhoto());
        }
        return AjaxResult.success(population);
    }

    /**
     * 新增人口
     */
//    @PreAuthorize("@ss.hasPermi('population:population:add')")
//    @Log(title = "人口", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody XlkjPopulation xlkjPopulation)
    {
        SysUser sysUser = sysUserService.selectUserByUserName(xlkjPopulation.getPhone());
        xlkjPopulation.setUserId(sysUser.getUserId());
        //设置默认审批状态
        xlkjPopulation.setStatus("1");
        return toAjax(xlkjPopulationService.insertXlkjPopulation(xlkjPopulation));
    }

    /**
     * 修改人口
     */
//    @PreAuthorize("@ss.hasPermi('population:population:edit')")
//    @Log(title = "人口", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody XlkjPopulation xlkjPopulation)
    {
        return toAjax(xlkjPopulationService.updateXlkjPopulation(xlkjPopulation));
    }

    /**
     * 删除人口
     */
    @PreAuthorize("@ss.hasPermi('population:population:remove')")
    @Log(title = "人口", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(xlkjPopulationService.deleteXlkjPopulationByIds(ids));
    }
}
